#include <QApplication>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QKeySequence>
#include <QStatusBar>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QModelIndex>
#include <QStandardItemModel>

#include "mainwindow.h"

MainWindow::MainWindow()
{
    QMenu *menu = new QMenu(tr("&File"), this);
    QAction *openAction = menu->addAction(tr("&Open..."));
    openAction->setShortcut(QKeySequence::Open);
    QAction *saveAction = menu->addAction(tr("&Save As..."));
    saveAction->setShortcut(QKeySequence::Save);
    QAction *quitAction = menu->addAction(tr("E&xit"));
    quitAction->setShortcut(QKeySequence::Quit);

    setupViews();
    setupModels();

    connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
    connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

    menuBar()->addMenu(menu);
    statusBar();
}

void MainWindow::openFile(const QString &path)
{
    QString fileName;
    if(path.isNull())
        fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"), "*.cht");
    else
        fileName = path;

    QFile file(fileName);
    if(file.open(QFile::ReadOnly | QFile::Text))
        return ;

    QTextStream stream(&file);
    QString line;

    int row = 0;
    do {
        line = stream.readLine();
        
        if(!line.isEmpty()) {
            m_model->insertRow(row, QModelIndex());
            
            QStringList pieces = line.split(",", QString::SkipEmptyParts);
            m_model->setData(m_model->index(row, 0, QModelIndex()), pieces.value(0));
            m_model->setData(m_model->index(row, 1, QModelIndex()), pieces.value(1)); 
            m_model->setData(m_model->index(row, 0, QModelIndex()), QColor(pieces.value(2)), Qt::DecorationRole);

            row ++;
        }

    } while(!line.isEmpty());

    file.close();
    statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000);
}

void MainWindow::saveFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Save As..."), "*.cht");
    if(fileName.isEmpty())
        return ;

    QFile file(fileName);
    if(!file.open(QFile::WriteOnly | QFile::Text))
        return ;

    QTextStream stream(&file);

    for(int row = 0; row < m_model->rowCount(QModelIndex()); ++row)
    {
        QStringList pieces;
        
        pieces.append(m_model->data(m_model->index(row, 0, QModelIndex()), Qt::DisplayRole).toString());
        pieces.append(m_model->data(m_model->index(row, 1, QModelIndex()), Qt::DisplayRole).toString());
        pieces.append(m_model->data(m_model->index(row, 0, QModelIndex()), Qt::DecorationRole).toString());

        stream << pieces.join(",") << "\n";
    }

    file.close();
    statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000);
}

void MainWindow::setupViews()
{
    m_model = new QStandardItemModel(8, 2, this);
    m_model->setHeaderData(0, Qt::Horizontal, tr("Label"));
    m_model->setHeaderData(1, Qt::Horizontal, tr("Quantity"));
}

void MainWindow::setupModels()
{
}

